package com.lill.system.web;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lill.common.component.util.PageUtils;
import com.lill.common.component.util.Query;
import com.lill.common.component.util.R;
import com.lill.support.annotation.CurrentUser;
import com.lill.system.constant.SystemConstant;
import com.lill.system.model.SysRoleEntity;
import com.lill.system.model.SysUserEntity;
import com.lill.system.service.ISysRoleDeptService;
import com.lill.system.service.ISysRoleMenuService;
import com.lill.system.service.ISysRoleService;

/**
 * 角色管理
 *
 * @author lill
 */
@RestController
@RequestMapping("/sys/role")
public class SysRoleController {
    @Autowired
    private ISysRoleService sysRoleService;
    @Autowired
    private ISysRoleMenuService sysRoleMenuService;
    @Autowired
    private ISysRoleDeptService sysRoleDeptService;

    /**
     * 角色列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("sys:role:list")
    public R list(@CurrentUser SysUserEntity curUser,@RequestParam Map<String, Object> params) {
        //如果不是超级管理员，则只查询自己创建的角色列表
        if (!SystemConstant.SUPER_ADMIN.equals(curUser.getId())) {
            params.put("companyId", curUser.getCompanyId());
        }

        //查询列表数据
        Query query = new Query(params);
        List<SysRoleEntity> list = sysRoleService.queryList(query);
        int total = sysRoleService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(list, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 角色列表
     */
    @RequestMapping("/select")
//    @RequiresPermissions("sys:role:select")
    public R select(@RequestParam Map<String, Object> map,@CurrentUser SysUserEntity curUser) {
        //如果不是超级管理员，则只查询自己所拥有的角色列表
    	if (!SystemConstant.SUPER_ADMIN.equals(curUser.getId())) {
    		map.put("companyId", curUser.getCompanyId());
        }
        List<SysRoleEntity> list = sysRoleService.queryList(map);

        return R.ok().put("list", list);
    }

    /**
     * 角色信息
     */
    @RequestMapping("/info/{roleId}")
//    @RequiresPermissions("sys:role:info")
    public R info(@PathVariable("roleId") String roleId) {
        SysRoleEntity role = sysRoleService.queryObject(roleId);

        //查询角色对应的菜单
        List<String> menuIdList = sysRoleMenuService.queryMenuIdList(roleId);
        role.setMenuIdList(menuIdList);

        List<String> domainIdList = sysRoleMenuService.queryDomainIdList(roleId);
        role.setDomainIdList(domainIdList);
        
        //查询角色对应的部门
        List<String> deptIdList = sysRoleDeptService.queryDeptIdList(roleId);
        role.setDeptIdList(deptIdList);

        return R.ok().put("role", role);
    }

    /**
     * 保存角色
     * @throws Exception 
     */
    @RequestMapping("/save")
//    @RequiresPermissions("sys:role:save")
    public R save(@CurrentUser SysUserEntity curUser,@RequestBody SysRoleEntity role) {
//        ValidatorUtils.validateEntity(role);
        role.setCreateUser(curUser.getId());
        int result=sysRoleService.save(role,curUser);
        if(result>0){
    		return R.ok();
        }else{
        	return R.failure();
        }
    }

    /**
     * 修改角色
     * @throws Exception 
     */
    @RequestMapping("/update")
//    @RequiresPermissions("sys:role:update")
    public R update(@CurrentUser SysUserEntity curUser,@RequestBody SysRoleEntity role) {
//        ValidatorUtils.validateEntity(role);

        role.setCreateUser(curUser.getId());
        int result=sysRoleService.update(role,curUser);
        if(result>0){
    		return R.ok();
        }else{
        	return R.failure();
        }
    }

    /**
     * 删除角色
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("sys:role:delete")
    public R delete(@RequestBody String[] roleIds,@CurrentUser SysUserEntity curUser) {
    	Map<String, Object> params=new HashMap<String, Object>();
    	params.put("ids", roleIds);
    	params.put("updateUser", curUser.getId());
    	params.put("updateTime", new Date());
    	
    	int result=sysRoleService.deleteLogicByMap(params);
    	if(result>0){
    		return R.ok();
        }else{
        	return R.failure();
        }
    }
    
    @RequestMapping("/copy")
    public R copy(@RequestBody String[] roleIds,@RequestParam String companyId,@CurrentUser SysUserEntity curUser) {
    	int result=sysRoleService.copyRoles(roleIds,companyId,curUser);
    	if(result>0){
    		return R.ok();
    	}else{
    		return R.failure();
    	}
    }
}
